package com.vue.admin.service.common.context;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: vue-admin-service
 * @description: 当前环境上下文
 * @author: zhangyangyang
 * @create: 2020-04-03 13:30
 **/
public class EnvironmentThreadContext {

    public static final String LANGUAGE_CONTEXT="languageCode";
    public static final String USER_INFO_CONTEXT="userInfoContext";
    private static ThreadLocal<Map<String,Object>> context=new ThreadLocal(){
        @Override
        protected Map<String,Object> initialValue() {
            return new HashMap<String,Object>();
        }
    };

    /**
     * 取得thread context Map的实例。
     *
     * @return thread context Map的实例
     */
    private static Map<String,Object> getContextMap() {
        return (Map<String,Object>) context.get();
    }

    /**
     * 清理线程所有被hold住的对象。以便重用！
     */

    public static void reset(){
        getContextMap().clear();
    }

    public static void put(String key,Object value){
        getContextMap().put(key,value);
    }

    public static Object remove(String key){
        return getContextMap().remove(key);
    }

    public static Object get(String key){
        return getContextMap().get(key);
    }
}
